﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GKQC.Common
{
    /// <summary>
    /// 抽样计划
    /// </summary>
    public class Aql
    {
        /// <summary>
        /// AQL數量計算 MIL-STD-1916
        /// </summary>
        /// <param name="qty">批量</param>
        /// <param name="qty0">0.01抽樣數</param>
        /// <param name="qty004">0.04抽樣數</param>
        /// <param name="qty01">0.1抽樣數</param>
        /// <param name="accept0">0.01允收數</param>
        /// <param name="accept004">0.04允收數</param>
        /// <param name="accept01">0.1允收數</param>
        /// <param name="reject0">0.01拒收數</param>
        /// <param name="reject004">0.04拒收數</param>
        /// <param name="reject01">0.1拒收數</param>
        public static void GetAQL_1916(double qty, out double qty0, out double qty004, out double qty01
            , out double accept0, out double accept004, out double accept01
            , out double reject0, out double reject004, out double reject01
            , AqlLevel aqlLevel
            )
        {
            qty0 = qty004 = qty01 = 0;
            accept0 = accept004 = accept01 = reject0 = reject004 = reject01 = 0;

            if (aqlLevel == AqlLevel.Ⅲ)
            {
                if (qty < 32) qty0 = qty004 = qty01 = qty;
                else if (qty <= 544) qty0 = qty004 = qty01 = 32;
                else if (qty <= 960) qty0 = qty004 = qty01 = 40;
                else if (qty <= 1632) qty0 = qty004 = qty01 = 48;
                else if (qty <= 3272) qty0 = qty004 = qty01 = 64;
                else qty0 = 64;
            }
            else if (aqlLevel == AqlLevel.Ⅴ)
            {
                if (qty < 192) qty0 = qty004 = qty01 = qty;
                else if (qty <= 1632) qty0 = qty004 = qty01 = 192;
                else if (qty <= 3072) qty0 = qty004 = qty01 = 256;
                else if (qty <= 5440) qty0 = qty004 = qty01 = 320;
                else if (qty <= 9216) qty0 = qty004 = qty01 = 384;
                else if (qty <= 17408) qty0 = qty004 = qty01 = 512;
                else qty0 = 512;
            }
            else
            {
                if (qty < 80) qty0 = qty004 = qty01 = qty;
                else if (qty <= 960) qty0 = qty004 = qty01 = 80;
                else if (qty <= 1632) qty0 = qty004 = qty01 = 96;
                else if (qty <= 3072) qty0 = qty004 = qty01 = 128;
                else if (qty <= 5440) qty0 = qty004 = qty01 = 160;
                else qty0 = 192;
            }
            //0
            accept0 = accept004 = accept01 = 0; 
            reject0 = reject004 = reject01 = 1;
        }

        public static double GetAQL_1916(double qty , AqlLevel aqlLevel, string customerCode)
        {
            double qty0 = 0;

            if (aqlLevel == AqlLevel.Ⅲ)
            {
                if (qty < 32) qty0 = qty;
                else if (qty <= 544) qty0 = 32;
                else if (qty <= 960) qty0 = 40;
                else if (qty <= 1632) qty0 = 48;
                else if (qty <= 3272) qty0 = 64;
                else qty0 = 64;
            }
            else if (aqlLevel == AqlLevel.Ⅴ)
            {
                if (qty < 192) qty0 = qty;
                else if (qty <= 1632) qty0 = 192;
                else if (qty <= 3072) qty0 = 256;
                else if (qty <= 5440) qty0 = 320;
                else if (qty <= 9216) qty0 = 384;
                else if (qty <= 17408) qty0 = 512;
                else qty0 = 512;
            }
            else
            {
                if (qty < 80) qty0 = qty;
                else if (qty <= 960) qty0 = 80;
                else if (qty <= 1632) qty0 = 96;
                else if (qty <= 3072) qty0 = 128;
                else if (qty <= 5440) qty0 = 160;
                else qty0 = 192;
            }

            return qty0;
        }

        /// <summary>
        /// 光宝性能测试抽样数
        /// </summary>
        /// <param name="qty"></param>
        /// <param name="aqlLevel"></param>
        /// <param name="customerCode"></param>
        /// <returns></returns>
        public static double GetAQL_1916_Guangbao_Electric(double qty, AqlLevel aqlLevel)
        {
            double qty0 = 0;

            if (aqlLevel == AqlLevel.Ⅴ)
            {
                if (qty < 64) qty0 = qty;
                else if (qty <= 3072) qty0 = 64;
                else if (qty <= 5440) qty0 = 69;
                else if (qty <= 9216) qty0 = 74;
                else if (qty <= 17408) qty0 = 81;
                else qty0 = 87;
            }
            else
            {
                if (qty < 44) qty0 = qty;
                else if (qty <= 1632) qty0 = 44;
                else if (qty <= 3072) qty0 = 49;
                else if (qty <= 5440) qty0 = 54;
                else if (qty <= 9216) qty0 = 54;
                else qty0 = 64;
            }

            return qty0;
        }

        /// <summary>
        /// 光宝尺寸测试抽样数
        /// </summary>
        /// <param name="qty"></param>
        /// <param name="aqlLevel"></param>
        /// <param name="customerCode"></param>
        /// <returns></returns>
        public static double GetAQL_1916_Guangbao_Size(double qty, AqlLevel aqlLevel)
        {
            double qty0 = 0;

            if (aqlLevel == AqlLevel.Ⅴ)
            {
                if (qty < 29) qty0 = qty;
                else if (qty <= 960) qty0 = 29;
                else if (qty <= 1632) qty0 = 32;
                else if (qty <= 3072) qty0 = 37;
                else if (qty <= 5440) qty0 = 41;
                else qty0 = 44;
            }
            else
            {
                if (qty < 18) qty0 = qty;
                else if (qty <= 544) qty0 = 18;
                else if (qty <= 960) qty0 = 20;
                else if (qty <= 1632) qty0 = 23;
                else if (qty <= 3072) qty0 = 26;
                else qty0 = 29;
            }

            return qty0;
        }

        /// <summary>
        /// 光宝外观测试抽样数
        /// </summary>
        /// <param name="qty"></param>
        /// <param name="aqlLevel"></param>
        /// <param name="customerCode"></param>
        /// <returns></returns>
        public static double GetAQL_1916_Guangbao_Apperance(double qty, AqlLevel aqlLevel)
        {
            double qty0 = 0;

            if (aqlLevel == AqlLevel.Ⅲ)
            {
                if (qty < 32) qty0 = qty;
                else if (qty <= 544) qty0 = 32;
                else if (qty <= 960) qty0 = 40;
                else if (qty <= 1632) qty0 = 48;
                else if (qty <= 3072) qty0 = 64;
                else qty0 = 80;
            }
            else if (aqlLevel == AqlLevel.Ⅴ)
            {
                if (qty < 192) qty0 = qty;
                else if (qty <= 1632) qty0 = 192;
                else if (qty <= 3072) qty0 = 256;
                else if (qty <= 5440) qty0 = 320;
                else if (qty <= 9216) qty0 = 384;
                else qty0 = 512;
            }
            else
            {
                if (qty < 80) qty0 = qty;
                else if (qty <= 960) qty0 = 80;
                else if (qty <= 1632) qty0 = 96;
                else if (qty <= 3072) qty0 = 128;
                else if (qty <= 5440) qty0 = 160;
                else qty0 = 192;
            }

            return qty0;
        }


        /// <summary>
        /// AQL數量計算
        /// </summary>
        /// <param name="qty">批量</param>
        /// <param name="qty0">0.01抽樣數</param>
        /// <param name="qty004">0.04抽樣數</param>
        /// <param name="qty01">0.1抽樣數</param>
        /// <param name="accept0">0.01允收數</param>
        /// <param name="accept004">0.04允收數</param>
        /// <param name="accept01">0.1允收數</param>
        /// <param name="reject0">0.01拒收數</param>
        /// <param name="reject004">0.04拒收數</param>
        /// <param name="reject01">0.1拒收數</param>
        /// <param name="custoemrCode">客戶代號</param>
        public static void GetAQL(double qty, out double qty0, out double qty004, out double qty01
            , out double accept0, out double accept004, out double accept01
            , out double reject0, out double reject004, out double reject01
            , string custoemrCode
            , AqlLevel aqlLevel
            )
        {
            qty0 = qty004 = qty01 = 0;
            accept0 = accept004 = accept01 = reject0 = reject004 = reject01 = 0;

            Aql.GetAQL_1916(qty, out qty0, out qty004, out qty01
            , out accept0, out accept004, out accept01
            , out reject0, out reject004, out reject0
            , aqlLevel
            );
        }

        /// <summary>
        /// AQL數量計算 MIL-STD-105E
        /// </summary>
        /// <param name="qty">批量</param>
        /// <param name="qty0">0.01抽樣數</param>
        /// <param name="qty004">0.04抽樣數</param>
        /// <param name="qty01">0.1抽樣數</param>
        /// <param name="accept0">0.01允收數</param>
        /// <param name="accept004">0.04允收數</param>
        /// <param name="accept01">0.1允收數</param>
        /// <param name="reject0">0.01拒收數</param>
        /// <param name="reject004">0.04拒收數</param>
        /// <param name="reject01">0.1拒收數</param>
        public static void GetAQL_105E(double qty, out double qty0, out double qty004, out double qty01
            , out double accept0, out double accept004, out double accept01
            , out double reject0, out double reject004, out double reject01
            )
        {
            qty0 = qty004 = qty01 = 0;
            accept0 = accept004 = accept01 = reject0 = reject004 = reject01 = 0;

            //0
            if (qty < 1250) qty0 = qty;
            else qty0 = 1250;
            //0.04
            if (qty <= 315) qty004 = qty;
            else if (qty >= 315 && qty < 150001) qty004 = 315;
            else qty004 = 1250;
            //0.1
            if (qty < 125) qty01 = qty;
            else if (qty >= 125 && qty < 10001) qty01 = 125;
            else if (qty >= 10001 && qty < 150001) qty01 = 500;
            else if (qty >= 150001 && qty < 500001) qty01 = 800;
            else qty01 = 1250;

            //0
            accept0 = 0; reject0 = 1;
            //0.04
            if (qty <= 150000)
            {
                accept004 = 0;
                reject004 = 1;
            }
            else
            {
                accept004 = 1;
                reject004 = 2;
            }
            //0.1
            if (qty <= 10000)
            {
                accept01 = 0;
                reject01 = 1;
            }
            else if (qty > 10000 && qty <= 150000)
            {
                accept01 = 1;
                reject01 = 2;
            }
            else if (qty > 150000 && qty <= 500000)
            {
                accept01 = 2;
                reject01 = 3;
            }
            else
            {
                accept01 = 2;
                reject01 = 3;
            }
        }


        /// <summary>
        /// 冠德抽样计划
        /// </summary>
        /// <param name="qty">批量</param>
        /// <param name="qty015">MA0.15抽样数</param>
        /// <param name="qty025">MI0.25抽样数</param>
        /// <param name="accept015">MA0.15允收数</param>
        /// <param name="accept025">MI0.25允收数</param>
        /// <param name="reject015">MA0.15拒收数</param>
        /// <param name="reject025">MI0.25拒收数</param>
        public static void GetAql_0728(double qty, out double qty015, out double qty025
            , out double accept015, out double accept025
            , out double reject015, out double reject025)
        {
            qty015 = qty025 = 0;
            accept015 = accept025 = reject015 = reject025 = 0;

            #region MA015
            if (qty <= 80)
            {
                qty015 = qty;
                accept015 = 0;
                reject015 = 1;
            }
            else if (qty > 80 && qty <= 3200)
            {
                qty015 = 80;
                accept015 = 0;
                reject015 = 1;
            }
            else if (qty > 3200 && qty <= 35000)
            {
                qty015 = 315;
                accept015 = 1;
                reject015 = 2;
            }
            else if (qty > 35000 && qty <= 150000)
            {
                qty015 = 500;
                accept015 = 2;
                reject015 = 3;
            }
            else if (qty > 150000 && qty <= 500000)
            {
                qty015 = 800;
                accept015 = 3;
                reject015 = 4;
            }
            else if (qty > 500000)
            {
                qty015 = 1250;
                accept015 = 5;
                reject015 = 6;
            }
            #endregion MA015

            #region MI025
            if (qty <= 50)
            {
                qty025 = qty;
                accept025 = 0;
                reject025 = 1;
            }
            else if (qty > 50 && qty <= 1200)
            {
                qty025 = 50;
                accept025 = 0;
                reject025 = 1;
            }
            else if (qty > 1200 && qty <= 10000)
            {
                qty025 = 200;
                accept025 = 1;
                reject025 = 2;
            }
            else if (qty > 10000 && qty <= 35000)
            {
                qty025 = 315;
                accept025 = 2;
                reject025 = 3;
            }
            else if (qty > 35000 && qty <= 150000)
            {
                qty025 = 500;
                accept025 = 3;
                reject025 = 4;
            }
            else if (qty > 150000 && qty <= 500000)
            {
                qty025 = 800;
                accept025 = 5;
                reject025 = 6;
            }
            else if (qty > 500000)
            {
                qty025 = 1250;
                accept025 = 7;
                reject025 = 8;
            }
            #endregion MI025
        }

        /// <summary>
        /// 美隆抽样计划
        /// </summary>
        /// <param name="qty">批量</param>
        /// <param name="qty025">MA0.25抽样数</param>
        /// <param name="qty015">MI1.00抽样数</param>
        /// <param name="accept015">MA0.25允收数</param>
        /// <param name="accept025">MI1.00允收数</param>
        /// <param name="reject015">MA0.25拒收数</param>
        /// <param name="reject025">MI1.00拒收数</param>
        public static void GetAql_0036(double qty, out double qty025, out double qty015
            , out double accept015, out double accept025
            , out double reject015, out double reject025)
        {
            qty025 = qty015 = 0;
            accept015 = accept025 = reject015 = reject025 = 0;

            #region MA025
            if (qty <= 50)
            {
                qty025 = qty;
                accept025 = 0;
                reject025 = 1;
            }
            else if (qty > 50 && qty <= 500)
            {
                qty025 = 50;
                accept025 = 0;
                reject025 = 1;
            }
            else if (qty > 500 && qty <= 1200)
            {
                qty025 = 80;
                accept025 = 0;
                reject025 = 1;
            }
            else if (qty > 1200 && qty <= 10000)
            {
                qty025 = 200;
                accept025 = 1;
                reject025 = 2;
            }
            else if (qty > 10000 && qty <= 35000)
            {
                qty025 = 315;
                accept025 = 2;
                reject025 = 3;
            }
            else if (qty > 35000 && qty <= 150000)
            {
                qty025 = 500;
                accept025 = 3;
                reject025 = 4;
            }
            else if (qty > 150000 && qty <= 500000)
            {
                qty025 = 800;
                accept025 = 5;
                reject025 = 6;
            }
            else if (qty > 500000)
            {
                qty025 = 1250;
                accept025 = 7;
                reject025 = 8;
            }
            #endregion MA025

            #region MI1.00
            if (qty <= 50)
            {
                qty015 = qty;
                accept015 = 0;
                reject015 = 1;
            }
            else if (qty > 50 && qty <= 150)
            {
                qty015 = 50;
                accept015 = 0;
                reject015 = 1;
            }
            else if (qty > 150 && qty <= 500)
            {
                qty015 = 50;
                accept015 = 1;
                reject015 = 2;
            }
            else if (qty > 500 && qty <= 1200)
            {
                qty015 = 80;
                accept015 = 2;
                reject015 = 3;
            }
            else if (qty > 1200 && qty <= 3200)
            {
                qty015 = 200;
                accept015 = 3;
                reject015 = 4;
            }
            else if (qty > 3200 && qty <= 10000)
            {
                qty015 = 200;
                accept015 = 5;
                reject015 = 6;
            }
            else if (qty > 10000 && qty <= 35000)
            {
                qty015 = 315;
                accept015 = 7;
                reject015 = 8;
            }
            else if (qty > 35000 && qty <= 150000)
            {
                qty015 = 500;
                accept015 = 10;
                reject015 = 11;
            }
            else if (qty > 150000 && qty <= 500000)
            {
                qty015 = 800;
                accept015 = 14;
                reject015 = 15;
            }
            else if (qty > 500000)
            {
                qty015 = 1250;
                accept015 = 21;
                reject015 = 22;
            }
            #endregion MI1.00
        }

        /// <summary>
        /// 欧陆通抽样计划
        /// </summary>
        /// <param name="qty">批量</param>
        /// <param name="qty0">抽样数</param>
        /// <param name="accept0">允收数</param>
        /// <param name="reject0">拒收数</param>
        public static void GetAql_0916(double qty, out double qty0)
        {
            qty0 = 0;

            #region MA025
            if (qty <= 80)
            {
                qty0 = qty;
            }
            else if (qty > 80 && qty <= 960)
            {
                qty0 = 80;
            }
            else if (qty > 960 && qty <= 1632)
            {
                qty0 = 96;
            }
            else if (qty > 1632 && qty <= 3072)
            {
                qty0 = 128;
            }
            else if (qty > 3072 && qty <= 5440)
            {
                qty0 = 160;
            }
            else if (qty > 5440)
            {
                qty0 = 192;
            }
            #endregion MA025
        }

        /// <summary>
        /// 富華抽样计划0372
        /// </summary>
        /// <param name="qty">批量</param>
        /// <param name="qty0">抽样数</param>
        public static void GetAql_0372(double qty, out double qty0)
        {
            qty0 = 0;

            #region 
            if (qty <= 1)
            {
                qty0 = 1;
            }
            else if (qty > 1 && qty <= 8)
            {
                qty0 = 2;
            }
            else if (qty > 8 && qty <= 15)
            {
                qty0 = 3;
            }
            else if (qty > 15 && qty <= 25)
            {
                qty0 = 5;
            }
            else if (qty > 25 && qty <= 50)
            {
                qty0 = 8;
            }
            else if (qty > 50 && qty <= 90)
            {
                qty0 = 13;
            }
            else if (qty > 90 && qty <= 150)
            {
                qty0 = 20;
            }
            else if (qty > 150 && qty <= 280)
            {
                qty0 = 32;
            }
            else if (qty > 280 && qty <= 500)
            {
                qty0 = 50;
            }
            else if (qty > 500 && qty <= 1200)
            {
                qty0 = 80;
            }
            else if (qty > 1200 && qty <= 3200)
            {
                qty0 = 125;
            }
            else if (qty > 3200 && qty <= 10000)
            {
                qty0 = 200;
            }
            else if (qty > 10000 && qty <= 35000)
            {
                qty0 = 315;
            }
            else if (qty > 35000 && qty <= 150000)
            {
                qty0 = 500;
            }
            else if (qty > 150000 && qty <= 500000)
            {
                qty0 = 800;
            }
            else if (qty > 500000)
            {
                qty0 = 800;
            }
            #endregion 
        }
    }

    /// <summary>
    /// 检验水准
    /// </summary>
    public enum AqlLevel
    {
        Ⅰ,
        Ⅱ,
        Ⅲ,
        Ⅳ,
        Ⅴ,
        Ⅵ,
        Ⅶ
    }
}
